package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.mapper.DesignMapper;
import com.example.hospitalsystem.mapper.LikesMapper;
import com.example.hospitalsystem.pojo.entity.Collects;
import com.example.hospitalsystem.pojo.entity.Design;
import com.example.hospitalsystem.pojo.entity.Likes;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.service.LikesService;
import com.example.hospitalsystem.utils.JwtHelper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class LikesServiceImpl extends ServiceImpl<LikesMapper, Likes> implements LikesService {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private DesignMapper designMapper;

    @Override
    public Result<Object> addLike(Integer designId) {
        String token=request.getHeader("Authorization");
        int userId=jwtHelper.getUserId(token);

        LambdaQueryWrapper<Likes> likesLambdaQueryWrapper=new LambdaQueryWrapper<>();
        likesLambdaQueryWrapper.eq(Likes::getDesignId,designId);
        Likes likes1=baseMapper.selectOne(likesLambdaQueryWrapper);

        if(likes1!=null){
            return Result.build("已经点赞过");
        }

        LocalDateTime time=LocalDateTime.now();
        Likes likes=new Likes();
        likes.setDesignId(designId);
        likes.setUserId(userId);
        likes.setLikeTime(time);
        this.save(likes);

        return Result.build(200,"点赞成功");
    }

    @Override
    public Result<Object> cancelLike(Integer designId) {

        LambdaQueryWrapper<Likes> likesLambdaQueryWrapper=new LambdaQueryWrapper<>();
        likesLambdaQueryWrapper.eq(Likes::getDesignId,designId);
        Likes likes1=baseMapper.selectOne(likesLambdaQueryWrapper);

        if(likes1==null){
            return Result.build("已经没有点赞了");
        }
        this.remove(likesLambdaQueryWrapper);
        return Result.build("取消点赞成功");
    }

    @Override
    public Result<Object> likeSate(Integer designId) {
        LambdaQueryWrapper<Likes> likesLambdaQueryWrapper=new LambdaQueryWrapper<>();
        likesLambdaQueryWrapper.eq(Likes::getDesignId,designId);
        Likes likes=baseMapper.selectOne(likesLambdaQueryWrapper);
        if(likes==null){
            return Result.build(0,"没有被点赞过");
        }
        return Result.build(1,"被点赞过");
    }

    @Override
    public Result<Object> getAllLikeInfo() {
        String token=request.getHeader("Authorization");
        int userId=jwtHelper.getUserId(token);
        LambdaQueryWrapper<Likes> likesLambdaQueryWrapper=new LambdaQueryWrapper<>();
        likesLambdaQueryWrapper.eq(Likes::getUserId,userId);

        List<Likes> likesList=baseMapper.selectList(likesLambdaQueryWrapper);

        List<Design> designList=new ArrayList<>();
        for(Likes likes:likesList){
            Design design=new Design();
            LambdaQueryWrapper<Design> designLambdaQueryWrapper=new LambdaQueryWrapper<>();
            designLambdaQueryWrapper.eq(Design::getDesignId,likes.getDesignId());
            design=designMapper.selectOne(designLambdaQueryWrapper);
            designList.add(design);
        }
        return Result.build(designList,200,"返回所有点赞信息成功");
    }

}
